package leeon.mobile.server.bbscache;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.utils.SystemProperty;

import leeon.mobile.BBSBrowser.models.BlockObject;
import leeon.mobile.BBSBrowser.models.BoardObject;
import leeon.mobile.server.bbscache.entity.DocMini;

/**
 * 执行任务队列用的servlet
 * @author leeon
 */
@SuppressWarnings("serial")
public class QueueServlet extends HttpServlet {
	
	private static final Logger logger = Logger.getLogger(QueueServlet.class.getName());
		
	private BBSCacheService service = new BBSCacheService();
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) 
			throws IOException {
		doit(req);
		resp.getWriter().print("OK");
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) 
			throws IOException {
		doit(req);
		resp.getWriter().print("OK");
	}
	
	private void doit(HttpServletRequest req) throws IOException {
		//生成模式，fdubbsapp的应用不能自动刷新数据
		boolean noauto = (SystemProperty.environment.value() == 
			SystemProperty.Environment.Value.Production)
			&& (SystemProperty.applicationId.get().equals("fdubbsapp"));
		
		String pathInfo = req.getPathInfo();
		
		if ("/refreshBoard".equals(pathInfo) && !noauto) {
			refreshBoard();
		} else if ("/deleteBoard".equals(pathInfo) && !noauto) {
			deleteBoard();
		} else if ("/deletePicture".equals(pathInfo) && noauto) {//bbsapp才需要delete pic数据
			new BBSUploadService().deleteExpiredPicture();
		} else if ("/refreshTopTen".equals(pathInfo)) {
			refreshTopTen();
		}
		
		String boardName = req.getParameter("board");
		String date = req.getParameter("fromDate");
		
		if (boardName == null || boardName.length() == 0) return;
		BoardObject board = Utils.findBoard(boardName);
		
		if ("/queueBoard".equals(pathInfo)) {
			service.refreshNewDoc(board);
		} else if ("/queueNew".equals(pathInfo)) {
			if (date == null) return;
			queueNew(board, Utils.stringToDate(date));
		} else if ("/queueDelete".equals(pathInfo)) {
			queueDelete(board);
		} else if ("/clearBoardEntity".equals(pathInfo)) {
			service.clearDoc(board);
		}
	}
	
	/**
	 * 刷十大
	 */
	private void refreshTopTen() {
		logger.info("start refresh TopTen");
		CacheService.getOrCreateTopTen(true, true);
		logger.info("end refresh TopTen");
	}
	
	
	/**
	 * 以下为刷版面文章的一些列方法
	 */
	private void refreshBoard() {
		List<BlockObject> all = CacheService.getOrCreateAll(false);
		for (BlockObject block : all) {
			refreshBoard(block.getAllBoardList());
		}
	}
	
	private void refreshBoard(List<BoardObject> list) {
		for (BoardObject b : list) {
			if (b.isDir()) {
				refreshBoard(b.getChildBoardList());
			} else {
				if (BBSBoardService.BOARD_WHITE_LIST.containsKey(b.getName()) || !BBSBoardService.USE_WHITE_LIST)
					QueueService.addBoardQueue(b);
			}
		}
	}
	
	private void queueNew(BoardObject board, Date fromDate) {
		List<List<DocMini>> ret = CacheService.getNewDocCache(board);
		if (ret == null) return;
		service.refreshNewDocQueue(board, fromDate, ret.get(0), ret.get(1));
	}
	
	
	
	/**
	 * 以下为删除版面文章的一些列方法
	 */	
	private void deleteBoard() {
		List<BlockObject> all = CacheService.getOrCreateAll(false);
		for (BlockObject block : all) {
			deleteExpiredBoard(block.getAllBoardList());
		}
	}
	
	private void deleteExpiredBoard(List<BoardObject> list) {
		for (BoardObject b : list) {
			if (b.isDir()) {
				deleteExpiredBoard(b.getChildBoardList());
			} else {
				if (BBSBoardService.BOARD_WHITE_LIST.containsKey(b.getName()) || !BBSBoardService.USE_WHITE_LIST)
					QueueService.addDeleteQueue(b);
			}
		}
	}
	
	private void queueDelete(BoardObject board) {
		service.deleteExpiredDoc(board);
	}
	
	



}
